package com.masterclient.app.modules.goods.newpage.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.eoner.baselibrary.bean.goods.CommodityDetailData;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;

public class NewCommodityDetailSpecAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    private Activity mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private CommodityDetailData data;
    private int mCount = 0;
    private boolean isShowMore;
    private static final int DEF_MAX_LENGTH = 4;

    // TODO 测试数据
    private static final String[] KEYS = {"产地", "产品成分", "存储方法", "适用人群", "是否收费", "史蒂芬"};
    private static final String[] VALUES = {"这里", "向日葵颜如意 青春素颜能量激活液", "Australia", "120ml/瓶", "测试测试数据测试测试数据", "胜多负少敌方"};

    public NewCommodityDetailSpecAdapter(Activity context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null,null);
    }

    public NewCommodityDetailSpecAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, CommodityDetailData data) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }
        ViewGroup ll_spec_group = holder.itemView.findViewById(R.id.ll_spec_group);
        ViewGroup ll_show_more = holder.itemView.findViewById(R.id.ll_show_more);

        if (!isShowMore && KEYS.length > DEF_MAX_LENGTH) {
            ll_show_more.setVisibility(View.VISIBLE);
            ll_show_more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    isShowMore = true;
                    notifyDataSetChanged();
                }
            });
        } else {
            ll_show_more.setVisibility(View.GONE);
        }

        ll_spec_group.removeAllViews();
        for (int i = 0; i < KEYS.length; i++) {
            if (!isShowMore && i >= DEF_MAX_LENGTH) {
                break;
            }
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.commodity_detail_item_spec, null);
            TextView tvKey = itemView.findViewById(R.id.tv_spec_key);
            TextView tvValue = itemView.findViewById(R.id.tv_spec_value);
            tvKey.setText(KEYS[i]);
            tvValue.setText(VALUES[i]);
            ll_spec_group.addView(itemView);
        }
    }

    @Override
    public int getItemCount() {
        return mCount;
    }
}
